package cn.com.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.entity.PexaminationPackage;

public class PexaminationPackageDao {

	private SessionFactory sessionFac;

	public SessionFactory getSessionFac() {
		return sessionFac;
	}

	public void setSessionFac(SessionFactory sessionFac) {
		this.sessionFac = sessionFac;
	}
	Session session;
	Transaction tr;
	//套餐查看全部
	public List<PexaminationPackage> selectAllPexaminationPackage(){
		session=sessionFac.openSession();
		String hql="from PexaminationPackage";
		Query query=session.createQuery(hql);
		@SuppressWarnings("unchecked")
		List<PexaminationPackage> list=query.list();
		if(list!=null){
			System.out.println("duidd");
		}
		return list;
	}
//这个是通过id查询，对象加了一个s   套餐
	public PexaminationPackage selectPexaminationPackageByid(PexaminationPackage pexaminationpackage){
		@SuppressWarnings("resource")
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		sessionFac=(SessionFactory)ctx.getBean("sessionFac");
		PexaminationPackage pexaminationPackagebyid=null;
		session=sessionFac.openSession();
		String hql="from PexaminationPackage where pexaminationpackageid=?";
		Query query=session.createQuery(hql);
		query.setInteger(0, pexaminationpackage.getPexaminationpackageid());
		@SuppressWarnings("unchecked")
		List<PexaminationPackage> list=query.list();
		if(list!=null){
			pexaminationPackagebyid=list.get(0);
		}
		return pexaminationPackagebyid;
	}
	//增加套餐
	public boolean addPexaminationPackage(PexaminationPackage pexaminationpackage){
		boolean b=false;
		try {
			session=sessionFac.openSession();
			tr=session.beginTransaction();
			session.save(pexaminationpackage);
			tr.commit();
			session.close();
			b=true;
			return b;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return b;
		}
	}
	//删除套餐
	public boolean deletePexaminationPackage(PexaminationPackage pexaminationpackage){
		boolean b=false;
		try {
			session=sessionFac.openSession();
			tr=session.beginTransaction();
			session.delete(pexaminationpackage);
			tr.commit();
			session.close();
			b=true;
			return b;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return b;
		}
	}
	//修改套餐
	public boolean updatePexaminationPackage(PexaminationPackage pexaminationpackage){
		boolean b=false;
		try {
			session=sessionFac.openSession();
			tr=session.beginTransaction();
			session.update(pexaminationpackage);
			tr.commit();
			session.close();
			b=true;
			return b;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return b;
		}
	}
}
